﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.WindowsForms.ToolTips;
using GMap.NET.MapProviders;

namespace GUI_Client_WCF
{
    public partial class ShowMap : Form
    {
        GMapOverlay overlayMarkers;
        GMapOverlay overlayRoutes;

        List<ServiceReference.BatteryCenterDistance> batteryList;

        public ShowMap(List<ServiceReference.BatteryCenterDistance> list)
        {
            InitializeComponent();

            batteryList = list;
        }

        private void googleMap_Load(object sender, EventArgs e)
        {
            googleMap.Position = new PointLatLng(56.3000, 9.9167);
            googleMap.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
            googleMap.MinZoom = 7;
            googleMap.MaxZoom = 7;
            googleMap.Zoom = 7;
            googleMap.Manager.Mode = AccessMode.ServerAndCache;

            overlayRoutes = new GMapOverlay(googleMap, "routes");
            overlayMarkers = new GMapOverlay(googleMap, "markers");

            List<GMapRoute> listGMapRoute = new List<GMapRoute>();
            double distance = 0.0;

            for (int routeID = 0; routeID < batteryList.Count; routeID++)
            {
                GMapMarker marker = null;

                if ((routeID + 1) < batteryList.Count)
                {
                    MapRoute mapRoute = GMapProviders.GoogleMap.GetRouteBetweenPoints(batteryList[routeID].BatteryCenter.name, batteryList[routeID + 1].BatteryCenter.name, false, false, 15);

                    listGMapRoute.Add(new GMapRoute(mapRoute.Points, "My route " + routeID.ToString()));

                    if (batteryList[routeID].Stop)
                        marker = new GMapMarkerGoogleRed(new PointLatLng(mapRoute.From.Value.Lat, mapRoute.From.Value.Lng));
                    else
                        marker = new GMapMarkerGoogleGreen(new PointLatLng(mapRoute.From.Value.Lat, mapRoute.From.Value.Lng));
                }
                else
                    marker = new GMapMarkerCross(new PointLatLng(listGMapRoute[listGMapRoute.Count - 1].To.Value.Lat, listGMapRoute[listGMapRoute.Count - 1].To.Value.Lng));

                marker.ToolTipText = batteryList[routeID].BatteryCenter.name + "\n" + (distance += batteryList[routeID].Distance) + "km";
                overlayMarkers.Markers.Add(marker);
            }

            foreach (var or in listGMapRoute)
                overlayRoutes.Routes.Add(or);

            googleMap.Overlays.Add(overlayRoutes);
            googleMap.Overlays.Add(overlayMarkers);
        }
    }
}
